<?php	
namespace app\pay\controller\yop;

use think\Controller;
use pay\yop\Config;

use pay\yop\lib\YopRequest;
use pay\yop\lib\YopClient3;
use customtool\Utils;

class Testdaifu extends Controller {
    private $cfg = null;
    
    public function _initialize() {
        $this->cfg = new Config();
    }        
    
    /*
     * 代付直连测试
     */
    public function index() {
        date_default_timezone_set('Asia/Shanghai');
        $orderId = 'DF' . date('YmdHis', time()) . rand(10, 99);
        $batchNo =  time(). rand(10000, 99999);
        $this->assign('orderId', $orderId);
        $this->assign('batchNo', $batchNo);
        return $this->fetch(); 
    }
    
    /*
     * 向平台发起请求
     */
    public function reqYopData() {
        $data = input('post.');
        $request = new YopRequest("OPR:" . $this->cfg->C('merchantId'), $this->cfg->C('private_key'), $this->cfg->C('yopUrl'), $this->cfg->C('yop_public_key'));
        $request->addParam("customerNumber",  $this->cfg->C('merchantId')); // 商户编号
        $request->addParam("groupNumber",     $this->cfg->C('parentMerchantId')); // 集团商户编号
        $request->addParam("batchNo",         $data['batchNo']); // 出款批次
        $request->addParam("orderId",         $data['orderId']); // 出款订单号
        $request->addParam("amount",          $data['amount']);  // 出款金额
        $request->addParam("product",         null); //  出款产品类，为空走委托结算出款
        $request->addParam("urgency",         0); // 是否加急，0：非加急   1：加急
        $request->addParam("accountName",     $data['accountName']); // 收款银行帐户名称
        $request->addParam("accountNumber",   $data['accountNumber']); // 收款银行账户卡号
        $request->addParam("bankCode",        $data['bankCode']); // 收款银行编号
        $request->addParam("bankName",        $data['bankName']); // 收款银行全称
        $request->addParam("bankBranchName",  $data['bankBranchName']); // 收款银行支行名称
        $request->addParam("provinceCode",    $data['provinceCode']); // 收款银行省编码
        $request->addParam("cityCode",        $data['cityCode']); // 收款银行城市编码
        $request->addParam("feeType",         $data['feeType']);  // 手续费方式，“SOURCE”商户承担; “TARGET”用户承担
        $request->addParam("desc",            $data['desc']); // 描述
        $request->addParam("leaveWord",       $data['leaveWord']); // 留言
        $request->addParam("abstractInfo",    $data['abstractInfo']); // 摘要
        $response = YopClient3::post($this->cfg->C('daifuUrl'), $request);
        if($response->validSign==1) { // 平台内部签名验证成功
            // 取得<易宝>返回结果
            $resData = Utils::objectToArray($response);
            // 根据返回结果更新 model('PayDaifuApply')表
            if( $resData['result'] == NULL) {
                return json_encode($resData['error'],320);
            } else {
                return json_encode($resData,320);
            }
        } else { // 平台内部签名验证失败
            return json_encode($response,320);
        } 
    }
}

